<style lang="scss" scoped>
	.content {

		width: 100%;
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.tip {
		color: #98A5B3;
		padding: 0 30rpx;
		width: 100%;
	}

	.nav {
		width: 100%;
		position: fixed;
		top: 0;
		left: 0;
	}

	.top {
		padding: 20rpx 0;
	}

	.search {
		width: 690rpx;
		height: 68rpx;
		background: #f4f5f7;
		border: 2rpx solid #f5f5f5;
		border-radius: 46rpx;
		margin: 0rpx auto;
		padding: 20rpx;

		.ipt {
			flex: 1;
		}
	}

	.footer {
		width: 100%;
		background-color: white;
		position: fixed;
		bottom: 0;
		left: 0;
		border-top: 1px solid #cccccc;
		padding-left: 30rpx;
		padding-right: 30rpx;
		z-index: 100;
	}

	.box {
		width: 690rpx;
		padding: 30rpx 20rpx;
		background: #ffffff;
		border-radius: 20rpx;
	
		.line {
			width: 100%;
			height: 100rpx;
			border-top: 1px solid #eeeeee;

			.ipt {
				width: 500rpx;
				text-align: right;
			}
		}

		.line2 {
			width: 100%;
			border-top: 1px solid #eeeeee;
			align-items: flex-start;

			.des {
				width: 500rpx;
				text-align: justify;
			}
		}

		.sbtn {
			width: 184rpx;
			height: 64rpx;
			background: #fffefe;
			border: 2rpx solid #409eff;
			border-radius: 34rpx;
		}

		.imgboxs {
			width: 100%;
			flex-wrap: wrap;

			.imgbox {
				width: 148rpx;
				height: 148rpx;
				border: 2rpx solid #dee2ee;
				border-radius: 10rpx;
				position: relative;
				margin-right: 18rpx;

				.photo {
					width: 100%;
					height: 100%;

				}

				.close {
					position: absolute;
					width: 32rpx;
					height: 32rpx;
					right: -16rpx;
					top: -16rpx;
				}
			}

			.imgbox:nth-child(4n) {

				margin-right: 0rpx;
			}
		}
	}

	.btns {
		.btn {

			padding: 10rpx 30rpx;
			border: 2rpx solid #409eff;
			border-radius: 34rpx;
		}
	}


	.mbtn {
		width: 330rpx;
		height: 88rpx;
		opacity: 1;
		border: 2rpx solid #409eff;
		border-radius: 46rpx;
		margin: 30rpx 0;
	}

	.gray {
		position: relative;
	}

	.red {
		position: absolute;
		top: 0rpx;
		left: -10rpx;
	}
	.sbtn {
		width: 176rpx;
		height: 88rpx;
		border: 2rpx solid #dee2ee;
		border-radius: 46rpx;
	}
	
	.lbtn {
		width: 484rpx;
		margin-right: 0;
	}
	
</style>

<template>
	<view class="content" v-if="plan">
		
		<view class="box" style="padding-bottom: 0;">
			<view class="textColor f32 bold p-bottom30">编辑任务</view>
			<view class="line between-flex f28">
				<view class="gray">父节点</view>
				<view >里程碑{{plan.parentIndex}}</view>
			</view>
			<view class="line between-flex f28">
				<view class="gray"><span class="red">*</span>任务名称</view>
				<input type="text" class="ipt" v-model="name" placeholder="请输入" />

			</view>
			<view class="line2 between-flex f28 p-top20">
				<view class="gray"><span class="red">*</span>任务描述</view>
				<textarea class="des textColor" style="height:120rpx;" v-model="remark" placeholder="请输入详细描述"
					placeholder-class="gray" />

			</view> 
			<view class="line between-flex f28">
				<view class="gray">预计工时（单位：天）</view>
				<input type="digit" class="ipt" style="width:300rpx;" v-model="planUse" placeholder="请输入" />
			
			</view>
			<view class="line between-flex f28">
				<view class="gray"><span class="red">*</span>开始时间</view>
				<view class="blue" @click="showStarttime=true">{{planStartDate?planStartDate:'请选择'}} <u-icon name='arrow-right'></u-icon>
				</view>
			</view>
			<view class="line between-flex f28">
				<view class="gray"><span class="red">*</span>结束时间</view>
				<view class="blue" @click="showEndtime=true">{{planEndDate?planEndDate:'请选择'}}<u-icon name='arrow-right'></u-icon>
				</view>
			</view>
			<view class="line between-flex f28">
				<view class="gray">审核人</view>
				<view class="blue" @click="peopleType=3; $navTo(PagesCourseRouterConfig.ChoosePerson+'?type=2')">
					{{auditUser?auditUser.name:'请选择'}}
					<u-icon name='arrow-right'></u-icon>
				</view>
			</view>
			<view class="line between-flex f28">
				<view class="gray">负责人</view>
				<view class="blue" @click="peopleType=2; $navTo(PagesCourseRouterConfig.ChoosePerson+'?type=2')">
					{{leaderUser?leaderUser.name:'请选择'}}
					<u-icon name='arrow-right'></u-icon>
				</view>
			</view>
			<view class="line between-flex f28 ">
				<view class="gray">参与人</view>
				<view class="blue align-flex" @click="peopleType=0; !person?$navTo(PagesCourseRouterConfig.ChoosePerson):$navTo(PagesCourseRouterConfig.ChoosePerson+'?users='+person.value)">
					<span v-if="!person">请选择</span>
					<span v-else class=" align-flex">
						<view style="max-width:370rpx;" class="one">{{person.value | users}}</view>
						等{{person.value.split(',').length}}人
					</span>
					<u-icon name='arrow-right'></u-icon>
				</view>
			</view>
			<view class="line between-flex f28">
				<view class="gray">关键任务</view>
				<view class="">
					<switch :checked="isHinge" @change="change($event,1)" color="#409EFF"/>
				</view>
			</view>
			<view class="line between-flex f28">
				<view class="gray">必须提交文档</view>
				<view class="">
					<switch :checked="isSubmit" @change="change($event,2)" color="#409EFF"/>
				</view>
			</view>
			<view class="line between-flex">
				<view class="gray">文件模板</view>
				<view class="btns align-flex">
					<view class="btn center-flex blue m-right20" @click="chooseImg">拍照</view>
					<view class="btn center-flex blue" @click="uploadfile">上传附件</view>
				</view>
			</view>
			
			<view class="line between-flex f28" v-for="item,index in enclosure" :key="index">
				<view class="align-flex blue">
					<u-icon name="file-text" color="#409EFF" size="38"></u-icon>
					{{item.fileName.substring(item.fileName.length-10)}}
				</view>
				<view class="gray">
					<u-icon name='close' @click="delfile(index)"></u-icon>
				</view>
			</view>
			
		</view>
		
		<u-picker v-model="showStarttime" mode="time" :params="params" @confirm="confirm($event,1)"></u-picker>
		<u-picker v-model="showEndtime" mode="time" :params="params" @confirm="confirm($event,2)"></u-picker>
		<view class="space"></view>
		<view class="footer between-flex paddingBottom" >
			<view class="sbtn center-flex textColor bold f32" @click="del">删除</view>
			<view class="lbtn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	import PagesCourseRouterConfig from '@/pagesCourse/routerConfig.js'
	export default {
		props: {
			
			list: {
				default: []
			}
		},
		data() {
			return {
				PagesCourseRouterConfig,
				dic: uni.getStorageSync('dic'),
				user: uni.getStorageSync('username'),
				navheight: 0,
				head: this.$head,
				name: '',
				remark: '',
				giveTime: '',
				planStartDate: '',
				planEndDate: '',
				peopleType: 0, //1提出人，2责任人，3审核人
				leaderUser: null, //责任人
				auditUser: null, //审核人
				enclosure: [], //附件
				planUse: 1,//计划工时
				person: null ,//参与人
				isHinge:false,//关键人物
				isSubmit:false,//是否提交文档
				params: {
					year: true,
					month: true,
					day: true,
					hour: true,
					minute: true,
					second: false
				},
				showStarttime: false,
				showEndtime: false,
				plan:{},
				project:uni.getStorageSync('creatProjectInfo'),
				parent:null
			}
		},
		created() {
			uni.$on('chooseplan',(data)=>{
				console.log(data)
				this.plan=data
				this.findParent()
				this.name=data.name
				this.remark=data.remark
				this.planUse=data.planUse
				if(data.startTime){
					this.planStartDate=data.startTime.substring(0,data.startTime.length-3)
				}
				
				if(data.endTime){
					this.planEndDate=data.endTime.substring(0,data.endTime.length-3)
				}
				
				this.enclosure=JSON.parse(data.template)
				this.isHinge=data.isHinge
				this.isSubmit=data.isSubmit
				if(data.reviewUser){
					this.auditUser={
						name:this.getUser(data.reviewUser),
						value:data.reviewUser
					}
				}
				if(data.principal){
					this.leaderUser={
						name:this.getUser(data.principal),
						value:data.principal
					}
				}
				if(data.userIds){
					this.person={
						name:this.getUser(data.userIds),
						value:data.userIds
					}
				}
				
				
			})
			
			uni.$on('choosePerson', (data) => {
				if (this.peopleType == 1) {
					this.giveUser = data
				} else if (this.peopleType == 2) {
					this.leaderUser = data
				} else if (this.peopleType == 3) {
					this.auditUser = data
				}else if(this.peopleType == 0){
					this.person=data
				}
			})
		},
		beforeDestroy() {
			uni.$off('choosePerson')
			uni.$off('chooseplan')
		},
		methods: {
			findParent(){
				this.list.forEach(item=>{
					if(item.id==this.plan.parentId){
						this.parent=item
					}
				})
				console.log('父任务',this.parent)
			},
			submit() {
				if (!this.name) {
					this.$msg('请输入任务名称！')
					return
				}
				if (!this.remark) {
					this.$msg('请输入任务描述！')
					return
				}
				
				if (!this.planEndDate) {
					this.$msg('请选择计划结束时间！')
					return
				}
				if (!this.auditUser) {
					this.$msg('请选择审核人！')
					return
				}
							
				if (!this.leaderUser) {
					this.$msg('请选择负责人！')
					return
				}
				
				
			
			
				var obj = {
					id:this.plan.id,
					name:this.name,
					parentId:this.plan.parentId,
					remark:this.remark,
					planUse:this.planUse,
					startTime:this.planStartDate,
					endTime:this.planEndDate,
					reviewUser:this.auditUser.value,
					principal:this.leaderUser.value,
					users: this.person?this.person.value:'',
					isHinge:this.isHinge?1:0,
					isSubmit:this.isSubmit?1:0
				}
				
				var enclosure = []
				this.enclosure.forEach(item => {
					enclosure.push({
						name: item.oldName,
						url: item.url
					})
				})
				obj['template'] = JSON.stringify(enclosure)
				console.log(obj)
				
				this.$showloading()
				this.$pop.ajaxPost('/busItemWork/updateChild',obj).then((res)=>{
					uni.hideLoading()
					if(res.code==0){
						this.$emit('submit')
					}else{
					this.$msg(res.msg)	
					}
				})
			},
			
			del(){
				this.$showloading()
				this.$pop.ajaxPost('/busItemWork/delete', {
					id:this.plan.id
				}).then(res => {
					uni.hideLoading()
					if(res.code==0){
						this.$emit('submit')
					}else{
					this.$msg(res.msg)	
					}
				})
			},
			confirm(e, index) {
				
				console.log(e)
				var timeText = e.year + '-' + e.month + '-' + e.day + ' ' + e.hour + ':' + e.minute
				var time = new Date((timeText+':00').replace(/-/g, '/')).getTime()//选择的时间
				
				var start=0//用于比较开始的时间
				var start1 = new Date((this.project.startTime+' 00:00:01').replace(/-/g, '/')).getTime() //项目开始时间
				var start2 =this.parent? this.parent.startTime?new Date(this.parent.startTime.replace(/-/g, '/')).getTime():0 :0;//父任务开始时间
				start1>start2?start=start1:start=start2;//开始时间取最大的日期
				var end =  0 //用于比较的结束时间
				
				var end1=0 //项目结束时间
				if(this.project.endTime){
					end1 = new Date((this.project.endTime+' 23:59:59').replace(/-/g, '/')).getTime()
				}
				var end2=0 //父任务结束时间
				if(this.parent&&this.parent.endTime){
					end2 = new Date(this.parent.endTime.replace(/-/g, '/')).getTime()
				}
				console.log(end1,end2)
				if(end1!=0&&end2!=0){
					end1<end2?end=end1:end=end2;//结束时间取最小时间
				}else if(end1==0&&end2==0){
					
				}else if(end1==0){
					end=end2
				}else{
					end=end1
				}
				
				console.log('end',end)
				if (index == 1) {//开始时间
					if(end){
						if(time<start){
							this.$msg('开始时间不能小于项目或父任务开始时间！')
						}else if(time>end){
							this.$msg('开始时间不能大于项目或父任务结束时间！')
						}else{
							this.planStartDate = timeText
						}
					}else{
						
						if(time<start){
							this.$msg('开始时间不能小于项目或父任务开始时间！')
						}else{
							this.planStartDate = timeText
						}
					}
					
				} else {//结束时间
					var startTime=new Date(this.planStartDate+':00').getTime()
					if(end){
						
						if(time<start){
							this.$msg('结束时间不能小于项目或父任务开始时间！')
						}else if(time>end){
							this.$msg('结束时间不能大于项目或父任务结束时间！')
						}else if(time<=startTime){
							this.$msg('结束时间不能小于或等于开始时间！')
						}else{
							this.planEndDate = timeText
						}
					}else{    
						if(time<start){
							this.$msg('结束时间不能小于项目或父任务开始时间！')
						}else if(time<=startTime){
							this.$msg('结束时间不能小于小于或等于开始时间！')
						}else{
							this.planEndDate = timeText
						}
					}
					
				}
			},
			getUser(value){
				console.log(typeof(value))
				
				if(typeof(value)=='object'){
					console.log(2222)
					var name=""
				}else{
					
					
					var arr=value.toString()
					var users=arr.split(',')
					var list = uni.getStorageSync('user')
					var array=[]
					users.forEach(item=>{
						array.push(list[item])
					})
					
					var name=array.join(',')
				}
				
				return name
			},
			
			change(e,type){
				if(type==1){
					this.isHinge=e.detail.value
				}else{
					this.isSubmit=e.detail.value
				}
			},
			
			delfile(index) {
				this.enclosure.splice(index, 1)
			},
			//选择图片
			async chooseImg() {

				var file = await this.$chooseImg()

				this.enclosure.push(file)

			},
			async uploadfile() {

				var file = await this.$uploadfile()
				this.enclosure.push(file)

			},
		}
	}
</script>
